#./main.py -- Main File starts from here.
#coding:utf-8


# Import Needs
from utils import log
import os, sys
import inspect
from constants import *


# Init Logging
debuger = log.Logger(os.path.join(LOG_FILE_PATH, "debug/"), level=DEBUG)
infoer = log.Logger(os.path.join(LOG_FILE_PATH, "debug/"))
errorer = log.Logger("&print", level=ERROR)
fataler = log.Logger("&win", level=FATAL)
loggerset = log.LoggerSet(debuger, infoer, errorer, fataler)


# System Info
loggerset.log("Main program started.", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
loggerset.log("Running Path: "+os.getcwd(), ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
loggerset.log("Python Interpretor Version: 3.8 (64-bit)", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
loggerset.log("Python Path: "+str(sys.path), ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)


# Security Check
loggerset.log("Starting Self Security Check...", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
try:    # Import Security Check
    loggerset.log("Try to import Self Security Check...", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
    import selfcheck

except Exception as e:  # Import Security Check Failed!
    loggerset.log(e,level=ERROR, ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
    loggerset.log("Security Check module not found!", level=ERROR, ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
    loggerset.log("No Security Check has been done... It may be harmful!", level=WARNING, ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)

else:
    try:    # Start Self Check
        loggerset.log("Starting Self Check...", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
        start = selfcheck.selfcheck(loggerset)
    except Exception as e:  # Self Check Failed!
        loggerset.log(e, level=ERROR, ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
        loggerset.log("Self Security Check failed!", level=FATAL, ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
        raise
    else:
        loggerset.log("Self Service Check success!", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)


# Start point
loggerset.log("Try to start mainfile startpoint...", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
try:
    start(loggerset)    # Try to start.
except ModuleNotFoundError:
    loggerset.log("No startpoints found!", level=FATAL, ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
    raise
except Exception as e:
    loggerset.log(e, level=FATAL, ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
    print("A fatal error happened...")
    print("Please check your ")
    raise


# End point
loggerset.log("Main program stopped.", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)